#include <sstream>
#include <ObjectInspectorDirectorBullet.h>
#include <ObjectInspectorItem.h>
#include <SceneViewerFunctions.h>

#include <BulletClassDataSource/PhysicsWorldBullet.h>
#include <BulletClassDataSource/RigidBodyBullet.h>
#include <QString>
#include <osg/CopyOp>
#include <EDesignerCore.h>
#include <osg/MatrixTransform>
#include <PhysicsWorld.h>
#include <NodeInfoContainerBullet.h>
#include <FunctionsPluginBullet.h>
ObjectInspectorDirectorBullet::ObjectInspectorDirectorBullet()
{
    QAction* actionCreatRigidBody = new QAction(QIcon("resource/action_bullet_logo.png"), QString("Creat Rigid Body"), 0);
    m_actionList.push_back(actionCreatRigidBody);

    QObject::connect(actionCreatRigidBody, SIGNAL(triggered()), this, SLOT(actionCreatRigidBody()));

    m_currentItem = NULL;
}

ObjectInspectorDirectorBullet::~ObjectInspectorDirectorBullet()
{

}

void ObjectInspectorDirectorBullet::rebuild(QStandardItem *model_root_item)
{
    EDesignerCore::instance()->simulationWaitUpdateEndAndStopSim();

    PhysicsWorldBullet* physicsWorld = new PhysicsWorldBullet();
    model_root_item->appendRow(physicsWorld->getObjItem());

    EDesignerCore::instance()->simulationstopWaitUpdateEnd();

}

bool ObjectInspectorDirectorBullet::addItemDown(EObject *parentObj, EObject *newObj)
{
    PhysicsWorldBullet* physicsWorldPtr = dynamic_cast<PhysicsWorldBullet*>(parentObj);
    if(physicsWorldPtr == NULL)return false;

    RigidBodyBullet* rigidBodyPtr = dynamic_cast<RigidBodyBullet*>(newObj);
    if(rigidBodyPtr == NULL)return false;

    rigidBodyPtr->rebuildRigidBody();


    PhysicsWorld::instance()->addRigidBody(rigidBodyPtr->getRigidBody());

    return true;
}

bool ObjectInspectorDirectorBullet::deleteItem(EObject *obj)
{
    RigidBodyBullet* rigidBodyPtr = dynamic_cast<RigidBodyBullet*>(obj);
    if(rigidBodyPtr == NULL)return false;

    return true;
}

void ObjectInspectorDirectorBullet::currentItemChanged(ObjectInspectorItem *item_ptr)
{
    m_currentItem = item_ptr;
}

QList<QAction *> ObjectInspectorDirectorBullet::getActionList()
{
    return m_actionList;
}

void ObjectInspectorDirectorBullet::actionCreatRigidBody()
{
    /**/
    QString nodeName;
    if(m_currentItem != NULL)
    {
        QString itemText = m_currentItem->text();
        QStringList itemTextStrList = itemText.split("-");
        if(itemTextStrList.size() == 2)
        {
            QString typeName = itemTextStrList[0];
            nodeName = itemTextStrList[1];
        }
    }

//    if(m_currentItem != NULL)
//    {
//        osg::MatrixTransform* mtPtr = dynamic_cast<osg::MatrixTransform*>((osg::MatrixTransform*)(m_currentItem->getObjectPtr()->getRelevantPtr()));
//        if(mtPtr != NULL)
//        {
//            mtPtr->getMatrix();
//            std::cout<<"ObjectInspectorDirectorBullet::actionCreatRigidBody: "<<m_currentItem->getObjectPtr()->metaObject()->className()<<std::endl;
//        }
//    }
    if(m_currentItem != NULL && nodeName.size() != 0 && FunctionsPluginBullet::findCollisionObjByMTName(nodeName.toStdString()) == NULL)
    {
        //std::cout<<"ObjectInspectorDirectorBullet::actionCreatRigidBody: "<<m_currentItem->getObjectPtr()->ifHasSuperClass("MatrixTransformOsg")<<std::endl;
        if(m_currentItem->getObjectPtr()->ifHasSuperClass("MatrixTransformOsg"))
        {
            NodeInfoContainerBullet* physicsWorldInfo = (NodeInfoContainerBullet*)(PhysicsWorld::instance()->getUserPointer());
            EObject* physicsEObj = physicsWorldInfo->getObjectInspectorItem()->getObjectPtr().data();

            RigidBodyBullet* rigidBodyEObj = new RigidBodyBullet();
            if(addItemDown(physicsEObj, rigidBodyEObj))
            {
                rigidBodyEObj->setTransformNodeName(nodeName);
                rigidBodyEObj->rebuildRigidBody();
                rigidBodyEObj->refreshItemTest();
            }
            physicsEObj->getObjItem()->appendRow(rigidBodyEObj->getObjItem());
        }
    }
}
